Sunday, July 8, 2018

[Python 3] Licensing : Mac address, Current date, and Change in system date


import re, uuid, datetime

class cm_license():
    def __init__(self,master):
  self.master = master

# Mac address
    def macAddress():
  ma = uuid.getnode()
temp_mac = re.findall('..','%012x' % uuid.getnode())
mac = ':'.join(temp_mac)
return mac
# Check the expiry date of the license
  def checkDate():
edate = datetime.date(2018,12,30)
current = datetime.datetime.now()
cdate = datetime.date(current.year, current.month, current.day)
date_diff = edate > cdate
return date_diff


if __name__ == '__main__':
# License
license_address = '00:e1:8c:eb:87:04' # Specifiy the mac address of pc
mac_name = cm_license.macAddress()
check_date = cm_license.checkDate()
# Run the code
if (mac_name == license_address) and (check_date == True):
main()
else:
messagebox.showinfo('License','Wrong physical address')



Thursday, July 5, 2018

[Python 3] Power supply control (KIKUSUI, Regulated DC Power Supply, PMX35-3A) via USB

1. Python library used
 1) pyvisa
    Frontend library
 2) pyvisa-py
    : Compatible library able to be used instead of the proprietary library NI-VISA
     Backend library
 3) pyusb (or libsub1)
   If you cannot detect usb drive with pyusb, then install the dependencies " libusb1 "
   - pip install pyusb for pyusb,
   - pip install libusb1 for libusb1
   : I did it after installing 'libusb1'
 4)  pyserial
 
For access to USB, both pyusb and pyserial are necessary to be installed.

2. KI-VISA ( for USB driver)
 It is compliant with IVI VISA specification 5.0, and the device driver for usb compatible products is installed automatically.
 After installing this, I had a power supply with python.

 file name : kivisa_5_5_0_275(x64).exe

3. Python 3 code

import visa

rm = visa.ResourceManager()
sl = rm.list_resources()
print(sl)

inst = rm.open_resource(sl[0]) # sl[0] is KIKUSUI power supply
# To automatically find a instrument
# serialno = '0x5555555'
# for sn in sl:
#     i = sn.find(serialno)
#     if i == 0:
#        inst = rm.open_resource(i)
inst.query('*IDN?')
inst.write('*IDN?')

# Preset, can avoid the initial error when connected to power supply 
inst.write("rst; status:preset; *cls")

# inst.write('SOUR:POW:MODE ON')

# inst.write("INST P6V") # Select +6V output
inst.write('VOLT 6.0') # Set output voltage to 6.0 V
inst.write('CURR 1.0') # Set output current to 1.0 A

# Most straightforward method
# to program the power supply over the remote interface
#  inst.write("APPL P6V, 6.0, 1.0")

# Output on/off
inst.write("OUTP OFF")
inst.write("OUTP ON")


Wednesday, July 4, 2018

[Python 3] String and Unicode

If any kind of text string is to be sent across the network, it needs to be encoded. This is why the server is using the " encode('ascii') " method on the data it transmits. Likewise, when a client receives network data, that data is first received as raw unencoded bytes. If you print it out or try to process it as text, we're unlikely to get what we expect. Instead, we need to decode it first. This is why the client code is using " decode('ascii') " on the result.

[Python 3] Python version management with virtual environment

1. virtualenv (On windows)
  • Install
    : pip install virtualenv
  • Create a folder
    : virtualenv venv
  • Activate a virtualenv
    : .\venv\Scripts\activate
  • Deactivate
    : Deactivate
  • Previous version
    : virtualenv venv --python=python3.5

2. pyvenv (manage multiple python 3.3 or later version)

3. autoenv
 automatically execute virtual environment

[Python 3] Python library

# In order to install python libraries, type
>> pip install

# Check the libraries installed
>> pip freeze

# Get the dependency libraries installed
>> pip freeze > reg.txt
# Install the same dependency libraries
>> pip install -r reg.txt

[Data science]
1. matplotlib
2. numpy
3. pandas
4. scipy

[Machine learning]
1. scikit-learn (pip install sklearn)

[Excel]
1. openpyxl
2. xlrd
3. xlwt
4. xlsxwriter

[Image processing]
1. OpenCV
2. OpenCV-contrib
3. Pillow
4. dlib (w/ cmake)

[CAN]
1. python-can : pip install python-can

[Database]
1. SQLite (default)

[GUI]
https://insights.dice.com/2017/08/07/7-top-python-gui-frameworks-for-2017-2/
1. tkinter (default)
2. wxpython (wxGlase, wxformbuilder)
3. PyQt : free license for not commercial use (w/ Qt designer)

[Standalone application]
1. Pyinstaller (recommend)
2. cx_Freeze
3. py2exe (for python 2.x)

[Python 3] Create CarMaker TestRun files with tkinter GUI

We can automate creating TestRun files of CarMaker.

GUI tool looks like below.



We need two files; one is an Excel where parameters are specified, and the other is a TestRun template file.

1. Read all parameters specified in Excel sheet
2. Find the same parameters in TestRun template file
3. Change values in TestRun template file into new values specified in Excel sheet
4. Create a new TestRun file
5. Repeat 1-4

carmaker_testrun

[Python 3] Using Pandas with Pyinstaller

When compiling python code including pandas library, if we have errors on pandas library,
we should do the following:

1. Go to the directory below
  c:/~/Python3.6/Lib/site-package/PyInstaller/hooks

2. Make a file named 'hook-pandas.py' in case that there is no such file.
  The contents should be as below.

 hiddenimports = ['pandas._libs.tslibs.timedeltas',
                             'pandas._libs.tslibs.nattype',
                             'pandas._libs.tslibs.np_datetime',
                             'pandas._libs.skiplist']

3. Then, type the following command on the cmd to make a standalone application
  > pyinstaller -F --noconsole [filename]