What are some strategies to write python code that works in CPython, Jython and IronPython

Having tries to target two of these environments at the same time I can safely say the if you have to use a database etc. you end up having to write unique code for that environment. Have you got a great way to handle this situation?


Asked by: Lily174 | Posted: 28-01-2022






Answer 1

If you do find you need to write unique code for an environment, use pythons

import mymodule_jython as mymodule

import mymodule_cpython as mymodule

have this stuff in a simple module (''module_importer''?) and write your code like this:

from module_importer import mymodule

This way, all you need to do is alter module_importer.py per platform.

Answered by: Lily891 | Posted: 01-03-2022



Answer 2

@Daren Thomas: I agree, but you should use the platform module to determine which interpreter you're running.

Answered by: Patrick521 | Posted: 01-03-2022



Answer 3

I write code for CPython and IronPython but tip should work for Jython as well.

Basically, I write all the platform specific code in separate modules/packages and then import the appropriate one based on platform I'm running on. (see cdleary's comment above)

This is especially important when it comes to the differences between the SQLite implementations and if you are implementing any GUI code.

Answered by: Edward367 | Posted: 01-03-2022



Answer 4

The #1 thing IMO: Focus on thread safety. CPython's GIL makes writing threadsafe code easy because only one thread can access the interpreter at a time. IronPython and Jython are a little less hand-holding though.

Answered by: Elian734 | Posted: 01-03-2022



Answer 5

I'm pretty sure you already know this but unfortunately Jython can't load c extension modules.

Answered by: Carlos351 | Posted: 01-03-2022



Answer 6

There are two major issues at play here...

Firstly, to my knowledge, only CPython has RAII - you have to close your own resources in Jython, Ironpython, etc.

And Secondly, as has been mentioned, is thread safety.

Answered by: Sam795 | Posted: 01-03-2022



Similar questions

python - Strategies for speeding up batch ORM operations in Django

One of my API calls can result in updates to a large number of objects (Django models). I'm running into performance issues with this since I'm updating each item individually, saving, and moving on to the next: for item in Something.objects.filter(x='y'): item.a="something" item.save() Sometimes my filter criterion looks like "where x in ('a','b','c',...)". It seems the


python - strategies for finding duplicate mailing addresses

I'm trying to come up with a method of finding duplicate addresses, based on a similarity score. Consider these duplicate addresses: addr_1 = '# 3 FAIRMONT LINK SOUTH' addr_2 = '3 FAIRMONT LINK S' addr_3 = '5703 - 48TH AVE' adrr_4 = '5703- 48 AVENUE' I'm planning on applying some string transformation to make long words abbreviated, like NORTH -> N, remove all spaces, commas and dashes an...


python - Strategies for debugging numerical stability issues?

I'm trying to write an implementation of Wilson's spectral density factorization algorithm [1] for Python. The algorithm iteratively factorizes a [QxQ] matrix function into its square root (it's sort of an extension of the Newton-Raphson square-root finder for spectral density matrices). The problem is that my implementation only converges for matrices of size 45x45 and smaller. So after 20 iterations, the summed ...


python - Strategies for Encryption with Django + Postgres?

I'm going to be storing a few sensitive pieces of information (SSN, Bank Accounts, etc) so they'll obviously need to be encrypted. What strategies do you recommend? Should I do all the encryption/decryption in the web app itself? Should I use something like pgcrypto and have the conversions done on the DB side? Something else entirely? Also, if you think I should do encryption on the web app side, what P...


windows 7 - What are common strategies for updating python programs?

I have a Windows program that I made with python and py2exe. I'd like to create an updating feature so that the software can be readily updated. What are common ways of going about this?


python - Strategies for solving inequality sudoku?

A twist on the classic sudoku solver that I've recently run into is the (rather crazy) inequality sudoku, which is your classic sudoku puzzle with the added twist that there are inequality conditions added to each box. Now, I've managed to scratch out a regular


python - rogue missing c++ symbol - debugging strategies?

I am building/using the python module for LAMMPS, which is the open source Molecular Dynamics simulator (project home, source). The python module works by compiling the C++ application as a library, and using CDLL/ctypes to call a C function interface. When you call the CDLL()


Strategies for searching through strings in python?

What are efficient ways to search for substrings in strings? - Are there specific functions built in python we can use? - Can we convert them to lists then access elements in the list? - Can we use for loops to search through individual elements? - Is there one generally accepted method by Python programmers? It would be helpful to understand the different strategies to help me solve the follow...


Unknown error from python while designing trading strategies

I am using python to design some trading strategies. My library includes Pyalgotrade and TA-lib mainly. I got an error from the code below. from pyalgotrade.tools import yahoofinance from pyalgotrade import strategy from pyalgotrade.barfeed import yahoofeed from pyalgotrade.technical import stoch from pyalgotrade import dataseries from pyalgotrade.technical import ma from pyalgotrade import technical from ...


python - Django 1.6.5 - Streaming Media Images strategies

We have a WYSIWYG editor what enables users to upload images via drag/drop or manual upload. As per standard Django practices, we save the image files from request.FILES to settings.MEDIA_ROOT, and the path in an ImageField. The contents of whatever the user added in the editor are chunked into pieces, and stored as such, for streaming purposes during reading by other users, which is done as an AJAX get request at...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top