Issues relating to file input - python

I am stuck with a problem and I would like to get input from you guys.
I am coding a Neo4J application using py2neo. I want to read a file and use that file to create the nodes and relationships
The problem I have is that the file input using code below, gives the lines back as a string.
file = "../create_db"
dbFile=open(file,'r')
And what I need is, instead of getting it back as a string, to get it raw.
At the moment the problem is that I want:
graph_db.create(node({'Id':'1', 'Description':'Computer'}))
But I get:
graph_db.create("node({'Id':'1', 'Description':'Computer'})")
Is there a way to get file input raw? Maybe an library that gives it back raw?
Thanks in advance,
Jiar

It seem your input file contains code statements (or partial code statements).
You can execute the statements using the eval builtin function and pass the results of that to the graph_db.create function.
However, you should be aware this allows arbitrary code to be executed (i.e. the input file becomes part of the executing script) and should be treated as part of the code (i.e. don't use an untrusted input file).

You could also check the ast module. Although I don't know if this will work in your case (emphasis mine):
ast.literal_eval(node_or_string)
Safely evaluate an expression node or a Unicode or Latin-1 encoded string
containing a Python expression.
The string or node provided may only consist of the following Python literal structures: strings,
numbers, tuples, lists, dicts, booleans, and None.
This can be used for safely evaluating strings containing Python expressions
from untrusted sources without the need to parse the values oneself.
So maybe if you have some control on the file to only use the dict part…
Using eval can be dangerous. Check also this question and its answers.

Related

Can we remove the input function's line length limit purely within Python? [duplicate]

I'm trying to input() a string containing a large paste of JSON.
(Why I'm pasting a large blob of json is outside the scope of my question, but please believe me when I say I have a not-completely-idiotic reason!)
However, input() only grabs the first 4095 characters of the paste, for reasons described in this answer.
My code looks roughly like this:
import json
foo = input()
json.loads(foo)
When I paste a blob of JSON that's longer than 4095 characters, json.loads(foo) raises an error. (The error varies based on the specifics of how the JSON gets cut off, but it invariably fails one way or another because it's missing the final }.)
I looked at the documentation for input(), and it made no mention of anything that looked useful for this issue. No flags to input in non-canonical mode, no alternate input()-style functions to handle larger inputs, etc.
Is there a way to be able to paste large inputs successfully? This would make my tool's workflow way less janky than having to paste into a file, save it somewhere, and then pass the file's location into the script.
Python has to follow the terminal rules. But you could use a system call from python to change terminal behaviour and change it back (Linux):
import subprocess,json
subprocess.check_call(["stty","-icanon"])
result = json.loads(input())
subprocess.check_call(["stty","icanon"])
Alternately, consider trying to get an indented json dump from your provider that you can read line by line, then decode.
data = "".join(sys.stdin.readlines())
result = json.loads(data)

Attempted regex of string that contains multiple numbers as different base counts

Currently building an encryption module in python 3.8 and have run into a snag with a desired feature/upgrade. Looking for some assistance in finding a solution that would be more helpful than writing a 'string crawler' to parse out an encrypted string of data.
In my first 'official' release everything works fine, but this is due to how much easier it is to split a string based of off easily identifiable prefixes in a string. For example, '0x' in a hexadecimal or '0o' in an octal.
The current definitions for what a number can inhabit use the aforementioned base types along with support for counts of 2-10 as '{n}bXX'.
What I currently have implemented works just fine for the present design, but having trouble with trying to come up with something that can handle higher bases (at least up to 64) that isn't going to be bulky or slow; also, the redesign is having trouble parsing out a string which contains multiple base counts, post assignment to their corresponding characters.
TL;DR - If I have an encoded string like so: "0x9a0o25179b83629b86740xc01d64b9HM-70o5521"
I would like it to be split as: [0x9a, 0o2517, 9b8362, 9b8674, 0xc0ld, 64b9HM-7, 0o5521]
and need help finding a better solution than: r'(?:0x)|(?:9b)|...'

Is there a way to convert a python dictionary string with some values as python commands?

How to convert this to a dictionary?
params = "{'cat_features':[X.columns.get_loc(i) for i in cat_vars],
'num_boost_round':100, 'eta':.01, 'reg_lambda':1.8, 'verbose':False,
'loss_function':'MultiClass','early_stopping_rounds':5}"
without the first part [X.columns.get_loc(i) for i in cat_vars] I can run ast.literal_eval(), but that doesn't work if there is python code in the string. Any idea how to solve this?
You can use plain eval.
However, using eval is is risky if the string comes from an non-trusted source because a properly crafted string could execute anything on the computer where the program is running.

Why does the output output of hashes look like \x88H\x98\xda(\x04qQ vs �*�r?

I'm new to encryption, and programming in general. I'm just trying to get my head wrapped around some basic concepts.
I'm using python, Crypto.Hash.SHA256
from Crypto.Hash import SHA256
In the REPL if I type
print SHA256.new('password').digest()//j���*�rBo��)'s`=
vs
SHA256.new('password').digest()//"^\x88H\x98\xda(\x04qQ\xd0\xe5o\x8d\xc6)'s`=\rj\xab\xbd\xd6*\x11\xefr\x1d\x15B\xd8"
What are these two outputs?
How are they supposed to be interpreted?
In the first case, you are using print, so Python is trying to convert the bytes to printable characters. Unfortunately, not every byte is printable, so you get some strange output.
In the second case, since you are not calling print, the Python interpreter does something different. It takes the return value, which is a string in this case, and shows the internal representation of the string. Which is why for some characters, you get something that is printable, but in other cases, you get an escaped sequence, like \x88.
The two outputs happen to just be two representations of the same digest.
FYI, when working with pycrypto and looking at hash function outputs, I highly recommend using hexdigest instead of digest.

Write Number as string csv python

I'm trying to write a csv file from json data. During that, i want to write '001023472' but its writing as '1023472'. I have searched a lot. But dint find an answer.
The value is of type string before writing. The problem is during writing it into the file.
Thanks in advance.
Convert the number to string with formatting operator; in your case: "%09d" % number.
Use the format builtin or format string method.
>>> format(1023472, '09')
'001023472'
>>> '{:09}'.format(1023472)
'001023472'
If your "number" is actually a string, you can also just left-pad it with '0''s:
>>> format('1023472', '>09')
'001023472'
The Python docs generally eschew % formatting, saying it may go away in the future and is also more finnicky; for new code there is no real reason to use it, especially in 2.7+.

Categories