Recently I want to manage some frequent used code and pacakge them and install them on cross platforms.

Creat a Project and Setup File

1
2
3
$mkdir cps
$cd cps
$touch setup.py

Creat Pacakge Folder

1
$mkdir cpssdk

Put your Code in Package

Go into the package folder

1
$cd cpssdk

creat a init file for package index

1
$touch __init__.py

then put your code in a python file

1
2
$touch cpstime.py
$vim cpstime.py

This is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from datetime import datetime
from datetime import timedelta
class CPSTime():
def parseTime(self,timeStr):
return datetime.strptime(timeStr,'%Y-%m-%d %H:%M:%S')

def parseTimeToStr(self,time_value):
return(time_value.strftime("%Y-%m-%d %H:%M:%S"))

def oneDayToSeconds(self,time_str):
time = datetime.strptime(time_str,'%H:%M:%S')
time_zero = datetime.strptime('00:00:00','%H:%M:%S')
return((time-time_zero).total_seconds())

def timeSlot(self,time=None,t_hour = None, t_min=None,t_sec = None):
divide_min = self.timeToMin(t_hour,t_min,t_sec)
start_min = self.timeToMin(time.hour,time.minute,time.second)
slot = int(start_min/divide_min)
return(slot)

def timeToMin(self,t_hour=None,t_min=None,t_sec=None):
total_min = 0
if t_hour is not None:
total_min = total_min + t_hour * 60
if t_min is not None:
total_min = total_min + t_min
if t_sec is not None:
total_min = float(total_min) + float(t_sec)/float(60)
return(total_min)

def timeDifferenceToSeconds(self,start_time,end_time):
return((end_time - start_time).total_seconds())

def changeTimeSlot(self,time_slot,orig_time_granularity,new_time_granularity):
new_time_slot = (time_slot * orig_time_granularity)/new_time_granularity
return(int(new_time_slot))

def timeSlotToTime(self,time_slot,time_slot_granularity=1,start_time = None):
if start_time is None:
time_zero = datetime.strptime('00:00:00','%H:%M:%S')
else:
time_zero = start_time
time_dif = timedelta(seconds = time_slot * time_slot_granularity)
time = time_zero+time_dif
# return(datetime.time(time))
return(time)

def equalTimeSeparator(self,start_time,end_time,interval_seconds = 600):
result = []
time_dif = timedelta(seconds = interval_seconds)
one_time = start_time
while(one_time < end_time):
result.append(one_time)
one_time = one_time + time_dif
result.append(one_time)
return(result)

Fill Package Setup Information

1
$vim setup.py

input following information

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
setup(
name = "cpstime",
version = "0.0.1",
keywords = ("pip", "datacanvas", "eds", "xiaoh"),
description = "cps time processing package",
long_description = "read time and calculate trip duration package in cyber physical system processing",
license = "MIT Licence",

url = "http://zhihanfang.com",
author = "ogre",
author_email = "ezhihan@gmail.com",

packages = find_packages(),
include_package_data = True,
platforms = "any",
install_requires = ['datetime']
)

The file tree is as follows.

1
2
3
4
5
└── cps
├── cpssdk
│   ├── __init__.py
│   └── cpstime.py
└── setup.py

Install and Test locally

1
$python setup.py install

Upload to PyPi

1
2
3
$ python setup.py register
$ python setup.py sdist # generate pip file
$ python setup.py sdist upload # generate pip file and upload

if you have authentication problem, create a ~/.pypirc file in your home directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[distutils]
index-servers =
pypi
pypitest

[pypi]
repository=https://upload.pypi.org/legacy/
username=your_username
password=your_password

[pypitest]
repository=https://testpypi.python.org/pypi
username=your_username
password=your_password

You may need to change repository url based on information returned.

Update your Code

Change the version and upload the package again.

Comments

2017-11-22