顯示具有 python 標籤的文章。 顯示所有文章
顯示具有 python 標籤的文章。 顯示所有文章

2024年6月25日 星期二

Uninstall failure on pipenv uninstall

Problem

Error "AttributeError: module 'enum' has no attribute 'IntFlag'" encountered when running unintall command of pipenv

Solutions

1. Run "pip uninstall enum34"
2. Re-run the uninstall again

2018年8月17日 星期五

[NodeJS] Import Module Without Executing the Script

When working on my nodeJS project, I encounter some kind of difficulties on the following scenario

I wrote an expressJS application, which in turn being allocated to be unit tested, the problem is I want to pass the expressJS application to my test program, so I figure out I can export the "app" from the "index.js", so I add

export default app


in my index.js, so finally I can export the app variable and import it to my unit test. But the problem is when I import the module, I also execute the code associated with module which is not what I want, is there anything like Python's if __name__ == "__main__"?

I just want to import the module without executing it, so I end up with the following strategies

I wrap all the actions in a function and return my app at the end

export function initApp() {
   app.use...
   app.use...
   return app;
}

Then I add this important snippet

if (require.main === module) {
    initApp();
}

This is important ! It tells nodeJS only if this file being run by node can the initApp being executed, so in my unit test case, I could just safely import this module without unintentionally execute the initApp function

2015年5月12日 星期二

cxfreeze Python program (Linux) - Wrapping the Python Program to Single Executable

I would like to note down here because Googling results frustrating with

  • Large amount of Windows environment Python application wrapping
  • Misleading and lack of example of cxfreeze API

But thanks for Stackoverflow, I finally get the solution despite various failure on different problems during the wrapping of my wxPython application.


Here is my situation
    I have written a wxpython program, and the program consist of not only .py files but with images and xml files, I want to 

  1. Wrap all these stuffs to one executable program in Ubuntu
  2. Deploy this program with deb installer

Solutions
  1. Download and install cxfreeze
  2. Please let me know but I cannot find relative commands on how to include external files, therefore I use the officially provided py script
    http://cx-freeze.readthedocs.org/en/latest/script.html
  3. Follow the below official guide and write the setup script to build the program
    http://cx-freeze.readthedocs.org/en/latest/distutils.html
  4. Run python setup.py build and your program will be built
  5. Follow the file structure to build the deb

  • mpstoolbox is the app name, name your own one
  • DEBIAN/control, DEBIAN/postinst is files to configure the deb package
  • usr/bin/..., this is the installed path, you should make your own path, all the files installed will follow this path structure
  • Finally, cd back to the folder (your app name), use dpkg -b yourAppName
  • A deb package will be generated
  • Use dpkg -i to install

Here comes the MOST important part, my problem

    It seems simple to follow the above to build the program, but indeed it doesn't, the guide does not teach you clearly on how to include the required modules to let you bring your program to other Linux machine to run, you need to follow the followings


  • binpathincludes parameter is important, please include all system library like /usr/lib, /usr/local/lib, or you may encounter this error libwx_gtk2u import error, ImportError: libwx_gtk2u_adv-2.8 etc. This is deal to the missing of wxPython library, you therefore need to include all required system library when building the app
  • About the setup.py, the api is not very clear in cxfreeze, follow stackoverflow's guide, thanks to the bro there, he also suffered from the lack of clearance of the api
    http://stackoverflow.com/questions/15486292/cx-freeze-doesnt-find-all-dependencies

    One thing here to pay attention to, the "include_files" attribute should be a list of tuples instead of single name only!! Your external files and other required images should also put it here.

2014年12月27日 星期六

[Django] Human readable text in admin panel

In Django, when we are dealing with the admin panel, we can specify human readable text for the column through different methods


  1. Make it when building the corresponding model
    e.g.1: question_text = models.CharField('question', max_length=200)
    e.g.2: pub_date = models.DateTimeField('date published')

  2. Django makes the first parameter as the field show in admin UI, in this case "Date published" and "Question" will be shown.

  3. Or, you can make it through the admin.py, Django's build-in admin model

    short_description 
  4. e.g.: was_published_recently.short_description = 'Published recently?'

    The corresponding column will be shown with readable text

  5. Or you let Django do the rest of the job using class option 'list_display'

    list_display = ('first_name', 'last_name')

    Django will set column name as First name and  Last name where they are both 'database fields', underscores will be evaluated as spaces

[Python] printf in Python

We usually make use of the following syntax for printf

print ("I am a %s %s", %('good', 'boy'))

[Python] Basic data types

There are several data structures in Python, the following are some key notes for reference


  1. List
    • Example: a=[1, 2, 3, 4, 5]
    • Mutable (support modification of objects like a.append(6)
    • Support stack operations like "pop", "append"
    • looping (1 expression only) squares = [x**2 for x in range(10)]
    • looping using traditional for
      for x in [1,2,3]:
          x = x**2
  2. Tuple
    • Example: tup = (1, 2, 3, 'wahaha') (Always enclosed with parentheses!)
    • Immutable -> tup[0] = 100 -> Not allowed!!
    • Can contain lists
      tup = (['a', 'b'], [1, 2])
    • Tuple with single element should add trailing comma
      tupSingle = (a,)
  3. Sets
    • Example: a = set([1, 2, 3])
    • No element duplicate
    • Example: 1 in a , Output: True (Fast testing)
    • Mutable
  4. Dictionary
    • Pretty similar to associative arrays in PHP
    • {} -> Empty dict
    • Mutable
    • Example: cord = {'x' : 123.4, 'y' : 278.5}
    • Get keys -> cord.keys() -> return list ['x', 'y']
    • Fast Testing -> 'in'

[Python] __main__ and __name__

A module in python (usually in form of .py) will be executed by Python interpreter, we normally face the following situations when running python code.


  1. Running the .py directly, like python manage.py
  2. Imported from another module, like from admin import manage.py

So how do python know whether the module is directly run as a main program or it is just a part of another module? The special variable __name__ will do the job!


Take a look at this piece of code

if __name__ == '__main__':
    print 'I am run by itself'
else:
    print 'I am imported as module'

So if the .py is being run as a main program, the __name__ will have value __main__ being set, otherwise, the __name__ 's value will become the module name itself. We can therefore deal with different situations depending on the program's executing behaviour.