The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

Archive for the ‘Python’ Category

What is ‘if __name__ == “__main__”‘ for?

Posted by jpluimers on 2019/08/14

One of the things when I learned Python was that in some scripts I found a block starting with a statement like this:

if __name__ == '__main__':

It looked like an idiom to me, and indeed it is: [WayBack] What is ‘if name == “main“‘ for?.

It allows a file to be both used as “main” standalone program file and a module. That section of code will not be executed if it is loaded as a module.

Part of the idiom is also to put your code in a separate method so this block is as short as possible.

if __name__ == '__main__':
main()

Via: [WayBack] Why is Python running my module when I import it, and how do I stop it? (thanks user166390 and Jeremy Banks for the answers there)

–jeroen

Posted in Development, Python, Scripting, Software Development | Leave a Comment »

What’s New In Python 3.0

Posted by jpluimers on 2019/07/30

Old, but I keep bumping in old Python code that needs conversion to work in Python 3.x: [WayBackWhat’s New In Python 3.0 — Python 3.6.3 documentation.

Via: [WayBack] Version 3: History of Python – Wikipedia

–jeroen

Posted in Development, Python, Scripting, Software Development | Leave a Comment »

Convert cURL command syntax to Python requests, Node.js code

Posted by jpluimers on 2019/07/26

Utility for converting curl commands to code

For my link archive: [WayBack] Convert cURL command syntax to Python requests, Node.js code

–jeroen

Posted in *nix, *nix-tools, cURL, Development, JavaScript/ECMAScript, Node.js, Power User, Python, Scripting, Software Development | Leave a Comment »

AlessandroZ/LaZagne: Credentials recovery project

Posted by jpluimers on 2019/04/15

Just when I thought I made a note of a password I hardly ever use, I didn’t, luckily this open source tools understands how to recover many kinds of passwords: AlessandroZ/LaZagne: Credentials recovery project.

–jeroen

Posted in *nix, *nix-tools, Chrome, Development, DVCS - Distributed Version Control, Firefox, git, Internet Explorer, Office, Opera, Outlook, Power User, Python, Scripting, Skype, Software Development, Source Code Management, Web Browsers, WiFi, Windows | Leave a Comment »

Python Data Science Handbook | Python Data Science Handbook

Posted by jpluimers on 2019/03/21

Cool stuff: [WayBackPython Data Science Handbook | Python Data Science Handbook.

Based on that I learned a lot of things on book publishing:

Via: [WayBack] You can read the Python Data Science Handbook by @jakevdp for free on his website  – ThisIsWhyICode – Google+

–jeroen

Posted in Development, Python, Scripting, Software Development | Leave a Comment »

GitHub – ArchiveTeam/googleplus-grab: Archiving Google+.

Posted by jpluimers on 2019/03/18

Soon this is a thing of the past, but for just a few more days, you can help: Archiving Google+.

Either run this project: [WayBack] GitHub – ArchiveTeam/googleplus-grab: Archiving Google+.

Or even better: run the appliance, and help the WayBack machine with any archiving projects setup by the virtual appliance: the [WayBack] ArchiveTeam Warrior – Archiveteam.

See some of their other pages for more background information:

You can donate both to the archive team, and the internet archive:

How is G+ archiving doing?

The tracker is well under way: [WayBack] Googleplus tracker Dashboard. History: archive.is 1; archive.is 2

Read the rest of this entry »

Posted in ArchiveTeamWarrior, Development, G+: GooglePlus, Google, Internet, InternetArchive, Power User, Python, Scripting, SocialMedia, Software Development, WayBack machine | Leave a Comment »

gdbgui – browser based debugger for C, C++, go, rust, Fortran. Modern gdb frontend.

Posted by jpluimers on 2019/03/05

[WayBack] gdbgui – browser based debugger for C, C++, go, rust, Fortran. Modern gdb frontend.: gdbgui (gnu debugger graphical user interface)

Via: [WayBack] Browser-based debugger for C, C++, go, rust, and more – written in Python with Flask. https://github.com/cs01/gdbgui Easy installation via PyPI: pip i… – Joe C. Hecht – Google+

–jeroen

Posted in C, C++, Debugging, Development, Fortran, GDB, Go (golang), Python, Scripting, Software Development | Leave a Comment »

Python 2.7 Countdown: a year from now it is unsupported

Posted by jpluimers on 2019/01/01

Besides wishing you a happy new year, also a reminder: [WayBack] Python 2.7 Countdown Python 2.7 will retire on januari 1, 2020. Learn more and see the countdown here.

This is indeed a breaking change for Python users, similar as from Perl 4 to Perl 5, and PHP 4 to PHP 5.

It shows two things:

  • how extremely hard it is to evolve a language without breaking things
  • how long it takes for the community at large to digest breaking changes

And indeed porting of complex systems is hard [WayBack] WIP: Port calibre to python 3 by flaviut · Pull Request #870 · kovidgoyal/calibre · GitHub but doable [WayBack] Bug #1714107 “Python 2 is retiring” : Bugs : calibre.

Via: [WayBack1/WayBack2] Python 3 improves in some ways over Python 2, but also makes a bunch of changes that are breaking, but cosmetic (i.e. renaming methods and functions, or… – Kristian Köhntopp – Google+ (with some interesting comments, but also a rant-sequence of someone who would better use that energy to improve Python than to bash it).

–jeroen

Posted in Development, Python, Scripting, Software Development | Leave a Comment »

Python threading: logging exceptions during the `run`

Posted by jpluimers on 2018/12/19

In A few observations on Python while I made my first steps into it, I mentioned the standard threading idiom in Python by wrapping the thread in a function. This has the drawback of having to catch and handle any exceptions in that function.

The higher level [WayBack] threading module has a [WayBack] Thread class with a [WayBack] run() method does not handle exceptions either.

For investigation of threading issues, it’s very convenient to know about the exceptions in a thread and their context.

So I’ve made a small base class that automagically logs any exceptions during a run:

import threading

from log import Log

class ExceptionLoggingThread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.logger = Log().getLogger()
        self.logger.debug("ExceptionLoggingThread().")

    def run_logic(self):
        self.logger.debug("Thread started.")

    def run(self):
        try:
            self.run_logic()
        except:
            self.logger.exception('Exception in `run`')
            raise

–jeroen

Posted in Development, Python, Scripting, Software Development | Leave a Comment »

Parsing simple html in Python

Posted by jpluimers on 2018/11/29

Was working to get fritzcap to emit a list of interfaces so I could specify which one to capture.

For that I needed to parse the output of http://fritz.box/capture.lua which consists of HTML fragments like below.

What I needed was for each consecutive entries of [WayBack] th and first [WayBackbutton tags:

  • content of the th tag
  • content of the value attribute of the button tag having a type="submit" attribute and name=start attribute

So before starting to work on it, I created [WayBackIn order to fix #5, print a list of available interfaces to potentially capture from · Issue #6 · jpluimers/fritzcap

The goal was to get a series of key/value pairs:

4-138 = AP2 (2.4 + 5 GHz, ath1) - Interface 1
4-137 = AP2 (2.4 + 5 GHz, ath1) - Interface 0
4-132 = AP (2.4 GHz, ath0) - Interface 1
4-131 = AP (2.4 GHz, ath0) - Interface 0
4-129 = HW (2.4 GHz, wifi0) - Interface 0
4-128 = WLAN Management Traffic - Interface 0a

So I built a class descending from [WayBackHTMLParser — Simple HTML and XHTML parser that ships with the [WayBackPython standard libraries.

If in the future I need more complex HTML parsing, then these links will help me choosing more feature rich parsers:

Back to the HTMLParser descendant in interfaces_dumper.py which can basically be condensed down to the code below.

  • handle_data is called for both start tags and end tags. The th value in data is only present in the start tag (at the time of end tag the data is empty), so you need to keep track of both last_start_tag and last_end_tag.
  • handle_endtag maintains last_end_tag to help handle_data.
  • handle_starttag maintains last_start_tag to help handle_data and also handles the button behaviour.
    • The buttonis only relevant if it has type="submit" and name="start" and a value attribute in that order.
    • Output is in data which is an array of key/value pairs.

Read the rest of this entry »

Posted in Development, Fritz!, Fritz!Box, fritzcap, Internet, Power User, Python, Scripting, Software Development | Leave a Comment »