Alteryx MD5_ASCII() function equivalent solution in Python - python

I'm not able to match the results of an MD5_ASCII() function in my Python program. Unfortunately I can't post the exact input string and results so please bear with me.
My code in Python looks something like this:
import hashlib
str_text = "SV6*123*TT"
str_hashed_1 = hashlib.md5(str_text.encode()).hexidigest()
The produced string does not match the MD5_ASCII() output. What is the correct way of doing this?

I've eventually found the issue. There was nothing wrong with the hashing function, the issue was with the input itself.

Related

Path.replace(target) does not work with my approach

Pathlib is suggested instead of os.module because is newer. However I find less documentation about Pathlib if I look for. I show an issue that give me problems.
Initial condition
In a directory I have the following files:
I want to change the prefix of the files "comune_min_corr_" in "zp".
OS approach
With the following code where df4 is dataframe that contain "comune":
for comune in df4:
os.replace('./Spedire/comune_min_corr_'+str(comune)+'.gpkg', './Spedire/zp_'+str(comune)+'.gpkg')
To be clear, "comune" in the following image are three items: Airolo, Alto Malcantone, Aranno. They are part of the filename.
The result is right, the files "zp_comune.gpkg" are replaced by the files "comune_min_corr_comune.gpkg" and "comune_min_corr_comune.gpkg" disappear:
Pathlib approach
I create again the "initial condition".
Then, I try to do the same thing than before, adapting for the "pathlib.replace(target)".
They say here https://realpython.com/python-pathlib/ that we have to use "source.replace(destination)" structure. I did with the following code.
for comune in df4:
Path=('Spedire')
('/comune_min_corr_'+str(comune)+'.gpkg').replace('/comune_min_corr_'+str(comune)+'.gpkg','/zp_'+str(comune)+'.gpkg')
It give me no error but it does not work, the content of the directory does not change
Question
Could you provide the solution to obtain the same result for pathlib that I obtain with os? Please explain your step and why it does not work for pathlib.
Thank you.
#Barmar I changed now and it works. Thank you
To resume:
with os.replace() it work without defining a variable because replace() is a function of the module "os".
with "path.replace" we have to do so:
for comune in df4:
variable = Path('Spedire')/f'comune_min_corr_{comune}.gpkg'
variable.replace(Path('Spedire')/f'zp_{comune}.gpkg')
In fact, we have to build an object (here called "variable") because "path.replace" is a class of the module "pathlib". Class always needs to have an object to obtain something because otherwise python does not know what we are talking about.
To be noted: I probably have done something similar, but I repeateadly worked confusing "gpkg" with "gpgk". This trivial mistake leads me in the wrong way.
If I told something wrong, please correct me.

Sublime Text 3 : How to display the line numbers of the evaluated lines in REPL Python?

using Sublime-Text-3
When i evaluate some selected python code, in PEPL Python (with SublimeREPL), i get something like that:
*>>> print("thank you")
thank you*
(i already set, "show_transferred_text": true,)
Instead of send and see the whole code that evaluated, i would like to see the line numbers of this evaluated code. Have you any ideas about this ?
(for example to display sth like this: >>> evaluated lines (1:30))
Thanks!
Unfortunately this is not possible with the default install of SublimeREPL. Additionally, the project is no longer being maintained, so until someone steps forward to steward the project, this feature won't get added unless you add it yourself. It's all open-source and written in Python.

Is there python code to convert nltk.tag taggedsent_to_conll (-U?) into conll-x format?

I'm uncertain whether taggedsent_to_conll output is in CoNLL-U format, although posts/replies by others seems to suggest that it is.
Today, I did a Google search with "conll-u to conll-x", got
https://github.com/UniversalDependencies/tools
README refers to the following perl script: conllu_to_conllx.pl

Reformat a date string with hyphens

I'm relatively new to python and would like to write clean code from the beginning. I need a parser of a string which formats a date in the form of "20140101" into "2014-01-01". How I achieved this is via:
def parse(date):
list_date = list(date)
list_date.insert(4,"-")
list_date.insert(7,"-")
return "".join(list_date)
which works perfectly fine but looks not very clean. Maybe there is no other solution but if there is a more pythonic way to code it would be appreciated if you would share this!
def parse(date):
return "{}-{}-{}".format(date[:4], date[4:6], date[6:])
Might have made a small error, but that should do the trick, given how you're doing this.
See also pyformat.info for more information about Python's string formatting.
Also, I suggest looking into the datetime module for more info about even more Pythonic ways of dealing with dates. ;)
You can use format, as mentioned by B. Eckles. But you can also do:
def parse(date):
return "%s-%s-%s" % (date[:4], date[4:6], date[6:])
Using format seems unnecessary as you aren't actually using it for anything more than substituting portions of your string.

Can the Python read() command be structured with two input variables?

I've been given some python code (at least I was told it was in python and it doesn't match matlab code structure) to get running and one of the lines is
data = f.read(1024x1024, 'int32')
I'm getting a syntax error which doesn't surprise me as I thought read() could only take one input and that was size...
I checked the docs https://docs.python.org/2/tutorial/inputoutput.html
and had a general look around, for example here:
http://www.tutorialspoint.com/python/python_files_io.htm and here: http://pymbook.readthedocs.org/en/latest/file.html
There are no indications that read() can take two inputs, nevermind one with a 'x' in it.
(I am also not clear on what the intentions of the 1024x1024 was, which is why I'm questioning if it's python, it looks like they're trying to set the size but it doesn't work like that for the read method)
Does anyone know what I'm missing? (or can work out what was originally meant by the command?)
Whole script section:
f = open(filename, 'r')
out = open(outfile, 'w')
data = f.read(1024x1024, 'int32')
result = out.write(data[0:256000])
out.closed
f.closed
It's basically notes on what they want to happen in a particular section of the script but they wrote it as if it was code and I have no idea what the intention of the data line is.
It looks more like pseudocode than anything; specifying "int32" makes me think they are reading from a binary file. You probably need something like
import numpy as np
def load_array(filename, dtype="int32", shape=(1024,1024)):
return np.fromfile(filename, dtype).reshape(shape)
Your syntax error has nothing to do with the read "command". Syntax error means that the interpreter/parser couldn't make sense of what you're writing at all. When that happens in python it will normally point at what's confusing the interpreter, fx:
data = f.read(1024x1024, 'int32')
^
SyntaxError: invalid syntax
Note the ^ pointing at 1024x1024 which is the fault, it simply doesn't understand what 1024x1024 is (so it won't get to the point to actually try to call the read method). If you meant to multiply the numbers you should have written 1024*1024 instead.
When you change to 1024*1024 you'll get other errors (for not reading the documentation for read - it doesn't take those arguments).
As for the language I'd suspect that there's no sane language with such a construct. The problem here is that x doesn't work well as a multiplication operator since that would be problematic with things like axe (did he mean a*e or the variable named axe?). It looks more like it's pseudo code.

Categories

Resources