Trouble iterating lines from a text file in Python for reuse - python-2.7

I am having trouble iterating a process in Python 2.7.
I have simplified it as much as possible to debug the first steps, and want to build on this.
If I specify the data
data = ("-4.916409,36.629535,3.721236,255,232,242")
And then get it to split
X,Y,Z,R,G,B = data.split(",")
I can recombine some of the elements to create new files and/or folders with the name of those elements:
RGB = (R + "+" + G + "+" + B)
os.makedirs(inputFolder + os.sep + RGB)
fo = open("Z:\\temp\\output" + os.sep + RGB + os.sep + RGB + ".txt", "w")
fo.write(X + "," + Y + "," + Z + "\n")
But when I try to do that from a longer text file, I can no longer combine the elements into this "RGB" as a file and/or folder name (only "R" or "G" or "B"), and only get a return for the first line.
inputFolder = ("Z:\\temp\\output")
coordinates = open("Z:\\temp\\accident2.txt", "r")
for line in coordinates:
X,Y,Z,R,G,B = line.split(",")
RGB = (R + "+" + G + "+" + B)
os.makedirs(inputFolder + os.sep + R)
fo = open("Z:\\temp\\output" + os.sep + R + os.sep + R + ".txt", "w")
fo.write(X + "," + Y + "," + Z + "\n")
But it works if I switch from the whole numbers to the decimals:
RGB = (X + "," + Y + "," + Z)
Then I can write:
fo = open("Z:\\temp\\output" + os.sep + RGB + os.sep + RGB + ".txt", "w")
Which is not quite right, but closer to what I want.
Why are numbers with decimals easier to "read" than those without?
How do I fix it so the whole numbers are treated like those with decimals?

well your coordinates is a file object, you need to read from this object. You can actually read line-by-line using readlines, function of file object. Check documentation
inputFolder = ("Z:\\temp\\output")
with open("Z:\\temp\\accident2.txt", "r") as coordinates:
for linenumber,line in enumerate( coordinates.readlines() ):
X,Y,Z,R,G,B = line[:-1].split(",")
RGB = (R + "+" + G + "+" + B)
try:
os.makedirs(inputFolder + os.sep + R)
fo = open("Z:\\temp\\output" + os.sep + R + os.sep + R + ".txt", "w")
fo.write(X + "," + Y + "," + Z + "\n")
except:
print "problem in line %d"%linenumber

Related

Substitute compound expression in SymPy

In sympy how can I make a substitution of a compound expression for a single variable as in the following example that only works for one of the instances of the common factor?
from sympy import *
x, y, z = symbols('x y z')
eq = Eq(2*(x+y) + 3*(x+y)**2, 0)
print(eq)
eq1 = Eq(z, x+y)
print(eq1)
eq2 = eq.subs(eq1.rhs, eq1.lhs)
print(eq2)
Output
Eq(2*x + 2*y + 3*(x + y)**2, 0)
Eq(z, x + y)
Eq(2*x + 2*y + 3*z**2, 0)
Desired output for last line
Eq(2*z + 3*z**2, 0)
Thanks to Oscar Benjamin's comment. I've solved the case I was actually interested in:
from sympy import *
t, L, C0, R, a, w0, h = symbols('t L C_0 R alpha omega_0 h')
Q = Function('Q')
ex0 = L*Q(t).diff(t, t) + R*Q(t).diff(t) + Q(t)*(1/(C0/(1+h*cos(a*t))))
print(ex0)
ex1 = ex0/L
ex1 = ex1.collect(Q(t)).expand()
print(ex1)
# substitute the following compound expression
ex2 = Eq(w0*w0, 1/(L*C0))
print(ex2)
ex3 = ex1.subs(L*C0, 1/(w0*w0))
ex4 = ex3.collect(Q(t))
print(ex4)
Output:
L*Derivative(Q(t), (t, 2)) + R*Derivative(Q(t), t) + (h*cos(alpha*t) + 1)*Q(t)/C_0
Derivative(Q(t), (t, 2)) + R*Derivative(Q(t), t)/L + h*Q(t)*cos(alpha*t)/(C_0*L) + Q(t)/(C_0*L)
Eq(omega_0**2, 1/(C_0*L))
(h*omega_0**2*cos(alpha*t) + omega_0**2)*Q(t) + Derivative(Q(t), (t, 2)) + R*Derivative(Q(t), t)/L
The substitution fails because subs does not encounter any argument x + y in the (sub)expression 2*(x + y): that expression automatically expands to 2*x + 2*y. So one solution is to do as Oscar suggested: make an algebraic substitution. I often follow this up with a restoration step to handle anything that didn't change as I expected. The other thing you can do is to use a helper function that groups together terms that are in the multi-term old object that you desire to replace:
def mvsubs(eq, old, new):
from sympy.core.exprtools import factor_terms
if not old.is_Add:
return eq.subs(old, new)
Add = old.func
free = old.free_symbols
for i in eq.atoms(Add):
reps = {}
for i in i.args:
if not all(i.has(x) for x in free):
reps.setdefault(i, Dummy())
eq = eq.subs(reps).subs(Add(*reps.values()),
factor_terms(Add(*reps.keys()))).subs(
old, new).xreplace({v:k for k,v in reps.items()})
return eq
>>> mvsubs(eq, x+y, z)
Eq(3*z**2 + 2*z, 0)

How can I fix this limit-related error in sympy?

I was evaluating a limit in sympy with a line like so:
print(i, sympy.limit(ys[i],t,sympy.oo))
And I got an error like so while it was working on the first equation, i=0:
Traceback (most recent call last):
File "schurSolver.py", line 101, in <module>
print(i, sympy.limit(ys[i],t,sympy.oo))
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/series/limits.py", line 67, in limit
return Limit(e, z, z0, dir).doit(deep=False)
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/series/limits.py", line 356, in doit
r = gruntz(e, z, z0, dir)
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/series/gruntz.py", line 709, in gruntz
r = limitinf(e0, z)
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/core/cache.py", line 70, in wrapper
retval = cfunc(*args, **kwargs)
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/series/gruntz.py", line 452, in limitinf
c0, e0 = mrv_leadterm(e, x)
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/core/cache.py", line 70, in wrapper
retval = cfunc(*args, **kwargs)
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/series/gruntz.py", line 545, in mrv_leadterm
f, logw = rewrite(exps, Omega, x, w)
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/series/gruntz.py", line 630, in rewrite
raise NotImplementedError('Result depends on the sign of %s' % sig)
NotImplementedError: Result depends on the sign of sign(233668289711193/25000000000000000 - 111705529447*I/4882812500000000)
I read in another question that restricting the independent variable t to be "positive=True" helps, but after trying that, I still get this error. The limit works with something simple like t->0 as well.
Does anyone know how to go about fixing this error? It looks like there is some issues with how a logarithm is evaluated for an imaginary number? But the original expression for ys[0] was automatically generated so its difficult to get into the weeds of what in it is causing this. Just in case this helps, when I print ys[0], it looks like so:
1.67916841526936e-25*exp(8.35132990598358e-25*t) + 3.0440740315262e-24*exp(1.06540862878089e-21*t) + 1.22059099713886e-26*(-76032.6108848118 + 59544.5018334113*I)*exp(6.70981298653122e-19*t) + 1.12181373255683e-7*exp(8.2018946160967e-7*t) + (-0.95449878958437 - 0.442530252270205*I)*(-1.84466407525497e-8 - 3.08484093798915e-19*I)*exp(t*(-203112508392540.0 - 2.52331886301774e-5*I)) + (5.61795388979922e-16 + 1.49904195929834e-19*I)*(0.00167414740678268 + 0.000547114383265314*I)*exp(t*(-114151373602122.0 - 1.44325505129479e-6*I)) + (-0.36913636076371 - 0.168200589234581*I)*(1.14987183065264e-9 - 1.14995840520071e-19*I)*exp(t*(-86299833488193.7 + 3.78746585889043e-6*I)) + (-3.98480722981375e-10 + 3.67085318739672e-20*I)*(0.194565978525368 + 0.0531020230452043*I)*exp(t*(-33018951594173.2 - 9.14621382779521e-6*I)) + (-3.7738632642937e-6 - 2.84357156584772e-19*I)*(0.00392424219163312 - 9.28185623096522e-5*I)*exp(t*(-24277348192864.8 - 2.72941776322116e-6*I)) + (1.78858827490596e-9 - 4.62685872832677e-20*I)*(0.102466662684398 + 0.030057437651088*I)*exp(t*(-19673632056099.5 - 1.74631349146584e-6*I)) + (-0.169400731132417 - 0.0463863266825838*I)*(-2.25915622691549e-9 + 1.50588535699424e-18*I)*exp(t*(-17910930373255.5 + 6.46900742695516e-6*I)) + (-1.69426864229786e-11 + 9.82878246622891e-21*I)*(0.00285877926702012 + 0.000933750646141991*I)*exp(t*(-17681708085870.8 + 1.70613111855344e-7*I)) + (-8.30971456066843e-9 - 4.22255208842594e-19*I)*(0.481476468508761 + 0.231055579027011*I)*exp(t*(-14522372847753.0 - 1.85817290336922e-6*I)) + (-0.461383182582166 - 0.214048879996138*I)*(1.31142240200075e-8 + 5.55533771042068e-19*I)*exp(t*(-10147742280220.5 - 1.62187999351383e-6*I)) + (-0.486661737657793 - 0.216114311889499*I)*(1.85862388324154e-8 + 3.3997137693952e-19*I)*exp(t*(-9531943441405.24 - 7.01631376653731e-6*I)) + (-1.76398625737487e-8 + 5.57602445821458e-19*I)*(0.195890299389504 + 0.0928169451470297*I)*exp(t*(-6608103143888.75 - 3.12137731289971e-6*I)) + (-0.300435936362988 + 0.0247885982038744*I)*(0.00738168551602585 + 2.42530069240965e-18*I)*exp(t*(-5916103451972.79 - 5.93855525314882e-7*I)) + (-0.00675418345802343 - 7.23316575610358e-5*I)*(4.32851594777732e-5 + 4.79289382606912e-18*I)*exp(t*(-4836034968430.14 + 7.75353476640173e-6*I)) + (-3.31764505444197e-8 - 2.09598249896058e-19*I)*(0.344479780628037 + 0.118030498879817*I)*exp(t*(-3496538884334.02 + 5.29295674301267e-7*I)) + (-5.25617796619618e-13 - 7.45250916749707e-21*I)*(0.00275707897077351 + 0.000141288946087623*I)*exp(t*(-3323772234744.91 - 3.6474988304411e-6*I)) + (-0.00542897659975518 - 0.000278206489217352*I)*(1.62397206394641e-12 + 5.56535845516503e-21*I)*exp(t*(-3206838936689.46 + 2.45613790946467e-6*I)) + (-0.0230498965839992 - 0.00753056844522449*I)*(4.54615850953473e-10 - 6.47080380894056e-20*I)*exp(t*(-2492604622850.55 - 7.51649523472664e-8*I)) + (-0.00619804860313521 - 0.000726559611443317*I)*(5.77080251380177e-5 - 6.02185415742892e-19*I)*exp(t*(-2217655702083.05 + 2.89708056827509e-7*I)) + (-0.000142522800335491 + 1.6450739955635e-20*I)*(0.124639068367501 + 0.0352274573126279*I)*exp(t*(-1263964845792.25 - 7.34981620628e-8*I)) + (-0.0455190179060991 - 0.00925775971342165*I)*(0.00086928483841261 + 9.31504280092746e-19*I)*exp(t*(-773908440127.084 - 1.86748361027625e-6*I)) + (-0.111403121551235 - 0.0345813110663439*I)*(-0.00233084874287111 - 1.15315828126713e-18*I)*exp(t*(-529268989287.646 - 4.12203336149994e-6*I)) + (-1.01585724021082e-15 - 3.09000238538058e-22*I)*(0.00176746139529834 + 0.000729361212377768*I)*exp(t*(-478200000155.486 - 9.16921973000672e-8*I)) + (-0.811883096583459 - 4.2550099822616e-15*I)*(-0.518993293127577 - 0.00972110044929831*I)*exp(t*(-388991609584.927 - 6.7250608293468e-7*I)) + (-0.0387540720033096 - 0.00944863416708929*I)*(-0.00187060769547946 - 1.44794168105828e-17*I)*exp(t*(-352508469777.172 + 2.865559999185e-8*I)) + (0.29631140399024 + 7.16988455778452e-15*I)*(0.612797163963353 - 0.0148620615751015*I)*exp(t*(-84463778017.8318 - 4.82408096227738e-7*I)) + (0.228198845971918 + 1.68004186976606e-14*I)*(0.356205437578597 - 0.0277336294045075*I)*exp(t*(-27107026132.9127 - 1.99992438106899e-6*I)) + (-0.0487385829569165 - 0.00249774689748956*I)*(1.86468385973065e-6 + 1.24279803043807e-18*I)*exp(t*(-25213033282.4521 + 4.66630438163317e-9*I)) + (-2.04278153200167e-6 - 1.08454612298207e-18*I)*(0.546005043078077 + 0.211890631348977*I)*exp(t*(-15836041264.2504 + 3.82335923932025e-7*I)) + (-0.16001447920409 - 0.0566482734214268*I)*(-0.0311186495944563 - 6.03688946605135e-15*I)*exp(t*(-10181103587.4674 - 8.40281999554162e-7*I)) + (-0.274707832808365 - 3.32877456730487e-13*I)*(-0.274665221392941 + 0.00389439184741831*I)*exp(t*(-1686410364.60416 - 3.7288640561267e-7*I)) + (0.287768671750691 + 8.16549479265847e-13*I)*(0.31506604587839 + 0.0411560159407836*I)*exp(t*(-717879484.692659 + 1.84416110560464e-7*I)) + (-0.091266349697144 - 1.52390329033371e-11*I)*(-0.0775768881495083 + 0.0206396810586052*I)*exp(t*(-12216564.1775522 + 3.3056220731526e-8*I)) + (-2.48572373065549e-7 - 1.18575541574023e-12*I)*(0.0723915049547977 - 0.118332634253011*I)*exp(t*(-400.634044372763 + 7.33509752734055e-11*I)) + (0.0397048947140555 + 1.31903562882084e-6*I)*(0.124733085774612 + 0.0562645148688352*I)*exp(t*(-57.5413328754333 - 7.22187055217595e-8*I)) + (-0.0934748414187167 - 0.000209131734565415*I)*(0.270392227999757 + 0.251283191343448*I)*exp(t*(-1.01145147301153 - 4.20542377050406e-5*I)) + (-0.202161461920234 - 0.0384145274277075*I)*(-0.0824721391984489 - 0.00271286338632531*I)*exp(t*(-0.00630349107976167 + 0.000307267943474572*I)) + (-0.125465988501229 + 0.0674620600301526*I)*(-0.0298308611815717 - 0.0251828521116607*I)*exp(t*(-0.0019205456655073 + 0.00180509000286347*I)) + (-0.121159947977535 - 0.0737856751721838*I)*(0.00212643434209978 + 0.0254754055239891*I)*exp(t*(-0.0018734497073853 - 0.00178036658087088*I)) + (-0.00196516569683774 + 0.0215263485842828*I)*(0.0410800376485034 + 0.160689514241982*I)*exp(t*(-0.00108161664395421 + 1.19430123636756e-6*I)) + (-0.0202920869563751 + 0.00106100069684705*I)*(-0.00779169899616668 + 0.00154197328770158*I)*exp(t*(-0.000593010359987407 - 3.86238217908242e-5*I)) + (-0.0646951328450709 - 0.0397891367685005*I)*(-0.0164606651475395 - 0.0174961689207555*I)*exp(t*(-0.000469971561489008 + 0.000767572303526128*I)) + (0.0178379629823442 - 0.014839268413719*I)*(0.128918118224738 + 0.0979644433573652*I)*exp(t*(-0.000449612109163136 - 0.000795598483467528*I)) + (0.00226837757770358 - 0.000994463857550389*I)*(0.00469456609139216 - 0.000935663565947804*I)*exp(t*(-0.000253173466113437 - 1.33316728772136e-9*I)) + (-0.00210634241308871 + 0.0109200919539407*I)*(-0.002103999532501 + 0.0109176696626757*I)*exp(t*(0.000183788647827431 + 0.000132411817609813*I)) + (-0.00975797212108995 + 0.00462796874285418*I)*(-0.00971161913606152 + 0.00466170117633005*I)*exp(t*(0.000183792089896646 - 0.000132430365770814*I)) + (-0.0238780404492042 - 0.0145154904924849*I)*(-0.00720496445224956 - 0.026515983959851*I)*exp(t*(0.000516688789621054 + 0.000387050742857334*I)) + (-0.0420944049105853 - 0.0641195080657347*I)*(-0.0335551849338057 - 0.0295925730945855*I)*exp(t*(0.000522024704024684 - 0.000383032189929175*I)) + (-0.0623495819266974 - 0.0967910744442665*I)*(-0.0385208326751541 - 0.001201727526135*I)*exp(t*(0.000731712217325226 + 0.00234124893486142*I)) + (-0.116642132297297 - 0.0573466554899272*I)*(-0.0381566622068165 + 0.000983531052733214*I)*exp(t*(0.000827457254100448 + 3.71786946011765e-6*I)) + (-0.0106542274642987 + 0.0382367626572694*I)*(0.175850479214185 + 0.0049312841730841*I)*exp(t*(0.000985016465311529 - 0.00255688765145607*I)) + (-0.138824440584369 + 0.261687056926164*I)*(-0.00944226247868757 + 0.0689966002025778*I)*exp(t*(0.00934673158844772 - 2.28772924307456e-5*I)) + (-8.02691775013775e-5 + 2.3285071861864e-7*I)*(0.0717823437666412 + 0.0476844488649595*I)*exp(t*(0.683054643685093 - 3.63677586488862e-9*I)) + (-0.000139235936271859 + 3.66048036224373e-7*I)*(0.0858393118234517 + 0.02638281491092*I)*exp(t*(0.753476907056178 - 6.49899851030096e-9*I)) + 5.04810441792277e-8*exp(-6.14767048612559e-9*t)

Collecting sub-expressions of a multivariate polynomial function in Sympy

I have a degree 6 multivariate equation in x and y written in Sympy, e.g.
eqn = a*x**6 + b*x**5*y + c*x**4*y + d*x**3*y + e*x**3*y**2 + ...
Is there a way to collect (x**2+y**2) and rearrange them into the following format?
eqn2 = A*(x**2+y**2)**3 + B*(x**2+y**2)**2 + C*(x**2+y**2) + D
A, B, C, D can be in x, y.
So far I have only tried collect(eqn, x**2 + y**2) and it returned the original equation.
Thank you!
Consider using a temporary symbol z = x**2 + y**2 and replace x**2 with z - y**2, then expand and restore:
>>> ex
A*x**6 + 3*A*x**4*y**2 + 3*A*x**2*y**4 + A*y**6 + B*x**4 + 2*B*x**2*y**2 +
B*y**4 + C*x**2 + C*y**2 + D
>>> ex.subs(x**2, z - y**2).expand().subs(z, x**2 + y**2)
A*(x**2 + y**2)**3 + B*(x**2 + y**2)**2 + C*(x**2 + y**2) + D
Although that works, perhaps a more direct thing to do is separate the expression by coefficients A-D and then factor those collections of terms:
def separatevars_additively(expr, symbols=[]):
free = set(symbols) or expr.free_symbols
d = {}
while free:
f = free.pop()
expr, dep = expr.as_independent(f, as_Add=True)
if dep.has(*free):
return None
d[f] = dep
if expr:
d[0] = expr
return d
>>> coeff = var("A:D")
>>> separatevars_additively(ex, coeff)
{B: B*x**4 + 2*B*x**2*y**2 + B*y**4, A: A*x**6 + 3*A*x**4*y**2 + 3*A*x**2*y**4 + A*y**6, D: D, C: C*x**2 + C*y**2}
>>> Add(*[factor(i) for i in _.values()])
A*(x**2 + y**2)**3 + B*(x**2 + y**2)**2 + C*(x**2 + y**2) + D

How to print an expression ordered by a symbol in sympy?

I tried to use init_printing(order=..) but none of those orders match what i need.
For example:
exp=m*x**3 + 3*m**2*x + x**2 + 1
print(exp,x) will give:
m*x**3 + x**2 + 3*m**2*x + 1
print(exp,m) will give:
3*x*m**2 + 3*x**3*m + x**2 + 1

Python: Trying to speed up a program that is running very slow

So, this program parses an e-mail address and a plain-text password from a text file. Then, it runs them through a few encryption routines and appends the encrypted text onto the end of the e-amil address:password entry in a new file.
import io
from Crypto.Cipher import AES
import base64
import struct
def str_to_a32(b):
if len(b) % 4:
b += '\0' * (4 - len(b) % 4)
return struct.unpack('>%dI' % (len(b) / 4), b)
def a32_to_str(a):
return struct.pack('>%dI' % len(a), *a)
def aes_cbc_encrypt(data, key):
encryptor = AES.new(key, AES.MODE_CBC, '\0' * 16)
return encryptor.encrypt(data)
def aes_cbc_encrypt_a32(data, key):
return str_to_a32(aes_cbc_encrypt(a32_to_str(data), a32_to_str(key)))
def base64urlencode(data):
data = base64.b64encode(data)
for search, replace in (('+', '-'), ('/', '_'), ('=', '')):
data = data.replace(search, replace)
return data
def a32_to_base64(a):
return base64urlencode(a32_to_str(a))
def stringhash(s, aeskey):
s32 = str_to_a32(s)
h32 = [0, 0, 0, 0]
for i in xrange(len(s32)):
h32[i % 4] ^= s32[i]
for _ in xrange(0x4000):
h32 = aes_cbc_encrypt_a32(h32, aeskey)
return a32_to_base64((h32[0], h32[2]))
def prepare_key(a):
pkey = [0x93C467E3, 0x7DB0C7A4, 0xD1BE3F81, 0x0152CB56]
for _ in xrange(0x10000):
for j in xrange(0, len(a), 4):
key = [0, 0, 0, 0]
for i in xrange(4):
if i + j < len(a):
key[i] = a[i + j]
pkey = aes_cbc_encrypt_a32(pkey, key)
return pkey
with io.open('user_list.txt', 'r') as file:
with io.open('user_list_enc.txt', 'a') as enc_file:
for line in file:
email_split, pass_split = line.replace('\n', '').split(":")
password_aes = prepare_key(str_to_a32(pass_split))
uh = stringhash(email_split.lower(), password_aes)
enc_file.write(email_split + ":" + pass_split + ":" + uh + "\n")
print email_split + ":" + pass_split + ":" + uh + "\n"