Python Forum
embedding data in a script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
embedding data in a script
#1
i have gotten AWS instances to run a Python script to do the instance initialization instead of bash. but this often needs to carry along chunks of data being used to create files on the instance. everything is in this one script and another script will be used to put it together and submit it. my first thought on bulk embedded data is triple quoted multiple lines of some kind of encoded data. what do others typically do in a case where a script will be creating some file (maybe binary, maybe text). can i do r""" to store raw binary such as compression output?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
You can use lexical strings concatenation to avoid triple quoted strings. Here is an example with data created by calling binascii.hexlify().decode() on raw binary data. I cut the hexlified string in chunks of 60 characters and write their Python representation on each line, with no comma between them, which concatenates them at lexical level.
data = binascii.unhexlify((
    '89504e470d0a1a0a0000000d4948445200000280000001e0080600000035'
    'd1dce40000003974455874536f667477617265004d6174706c6f746c6962'
    '2076657273696f6e332e342e312c2068747470733a2f2f6d6174706c6f74'
    '6c69622e6f72672fd9d40fa0000000097048597300000f6100000f6101a8'
    '3fa7690000a02849444154789cecdd777814e5dac7f1efee66d37b2f0442'
    ).encode())
Skaperen likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  embedding python Skaperen 0 594 Apr-22-2020, 03:19 AM
Last Post: Skaperen
  embedding Python script in shell scripts Skaperen 5 4,527 Mar-23-2018, 01:07 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020