Does someone have a nice tip how to port tis PHP function to python?
/**
* converts id (media id) to the corresponding folder in the data-storage
* eg: default mp3 file with id 120105 is stored in
* /(storage root)/12/105/default.mp3
* if absolute paths are needed give path for $base
*/
public static function id_to_location($id, $base = FALSE)
{
$idl = sprintf("%012s",$id);
return $base . (int)substr ($idl,0,4) . '/'. (int)substr($idl,4,4) . '/' . (int)substr ($idl,8,4);
}
For python 2.x, you have these options:
[best option] The newer str.format and the full format specification, e.g.
"I like {food}".format(food="chocolate")
The older interpolation formatting syntax e.g.
"I like %s" % "berries"
"I like %(food)s" % {"food": "cheese"}
string.Template, e.g.
string.Template('I like $food').substitute(food="spinach")
You want to use the format() method for strings in Python 3:
http://docs.python.org/library/string.html#formatstrings
or check the string interpolation documentation for Python 2.X
http://docs.python.org/library/stdtypes.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With