Python code

Here, I store any Python code that I made. There modules, really.


A module that takes multiple files and puts them in one. It's probably good if you're making a game with many sounds and want to encrypt them.

Download pack


It's not very hard to use. First, import it.
'import pack'
Then, create a new instance.
'my_packfile=pack.pack()' The constructor takes no parameters. Methods you can use are: Note: Ignore the wierd way I wrote the function declarations.
bool add(string filename): Adds filename to the pack and returns true on success, or raises an exception if the file can't be found.
string[] get_files(): Gets the list of files stored in this pack and returns a list of strings.
bool extract(string filename, string dest): Extracts filename to the file dest, overwriting it if it exists. Returns true on success and prints an error to sys.stdout on failure. It doesn't raise an exception though, probably..
bool generate(string filename, string key): Writes the pack files data to filename, using key to encrypt the data. Key can be of any length.
bool load(string filename, string key): Loads a pack data created by generate() from filename, using key to decrypt the data. Returns true on success. Raises key error if the key is invalid. Will not work properly if you don't load a valid pack file.
bytes get_bytes(string filename): Returns the decrypted, decompressed contents of filename from the pack file if it's there, or raises an error.
module run_as_bytecode(string filename, string name='pack_file'): Assuming filename is a pyc (compiled Python bytecode file) inside the pack file currently loaded or being made, attempts to return a module, of the sort import os returns for example, that you can then use as a normal imported module. Note that if this module requires other modules in the pack , there is currently no easy way for them to all be imported at once, unless you want to try and use sys.modules.

Other special things:
You can use the len() function on a pack instance, which returns the number of files it has.
Note: Unless I specifically note that pack files are incompatible, upgrading to a new version of this module will not stop you from loading your pack files.


Some code to show you how to do some things with a pack file

Make a simple pack file and add meow1.wav and meow2.wav to it

from pack import pack #Import the pack class
file=pack() # Make a new pack file object
file.add("meow1.wav") # Adds meow1.wav to the pack. It can be in the current directory, or it can be in a subfolder. Once you do this, pack.get_files() will show this file in the pack.
file.add("meow2.wav") # Same as above.
file.generate("sounds.dat", "catsarecute") # This creates the actual pack file with all the files you just added to it. It is then encrypted with the key 'catsarecute'

Load up the pack file and extract useful data

from pack import pack
file.load("sounds.dat", "catsarecute") # Assuming the file sounds.dat exists, this will try to load it, and decrypt it with the key 'catsarecute', which is the key you used in the last example. If everything worked, the function returns true.
file.get_files() # You should see ['meow1.wav', 'meow2.wav'] file.extract("meow1.wav", "meow1.wav") # Takes the file meow1.wav in the pack you just loaded, and extracts it back onto disk where you can open and play it. file.get_bytes("meow1.wav") # Returns a lot of jibberish, which is the stored data of the file in the pack.
And, that's it for the pack module! Enjoy!