I'm worked on PHP previously and want to know python web programming. But everything for me is ambiguous.
Is it possible to program web app using python without "any" framework?
I know frameworks make everything easy for us and even if it is possible program web app without framework , it is not logical for big projects. But my target is just learning because I think start programming with frameworks is not good idea and we should know some information about pure python in web applications. So Imagine I want make very very simple webpage like hello world or small counter or small api. please do not offer me lightweight frameworks like flask. I just want know can pure python create webpages?
Yes, technically, you can create a webpage with "pure python," but you certainly wouldn't want to! The most beautiful part of programming is the fact that there are millions of others who have worked hard to create "frameworks," such as Flask or Django, that massively simplify the work that is required to make an application.
If your goal is "just learning" then using a framework that you are unfamiliar with is a great way to start. Learning programming is not about the technical knowledge required to run every aspect of your code, rather it is all about knowing how to ask the right questions for the particular application you are trying to build.
Programmers are lazy. If there is a shortcut that can be taken in code, you should take it (unless you have a really good reason not to).
Think of this example:
I have a list that was converted to a string:
myStringList = "['foo', 'bar', 'foobar']"
How can I convert this string to a format that my computer can interpret as a list?
I could make a function like this:
def stringToList(node_or_string):
if isinstance(node_or_string, str):
node_or_string = parse(node_or_string, mode='eval')
if isinstance(node_or_string, Expression):
node_or_string = node_or_string.body
def _convert_num(node):
if isinstance(node, Constant):
if isinstance(node.value, (int, float, complex)):
return node.value
elif isinstance(node, Num):
return node.n
raise ValueError('malformed node or string: ' + repr(node))
def _convert_signed_num(node):
if isinstance(node, UnaryOp) and isinstance(node.op, (UAdd, USub)):
operand = _convert_num(node.operand)
if isinstance(node.op, UAdd):
return + operand
else:
return - operand
return _convert_num(node)
def _convert(node):
if isinstance(node, Constant):
return node.value
elif isinstance(node, (Str, Bytes)):
return node.s
elif isinstance(node, Num):
return node.n
elif isinstance(node, Tuple):
return tuple(map(_convert, node.elts))
elif isinstance(node, List):
return list(map(_convert, node.elts))
elif isinstance(node, Set):
return set(map(_convert, node.elts))
elif isinstance(node, Dict):
return dict(zip(map(_convert, node.keys),
map(_convert, node.values)))
elif isinstance(node, NameConstant):
return node.value
elif isinstance(node, BinOp) and isinstance(node.op, (Add, Sub)):
left = _convert_signed_num(node.left)
right = _convert_num(node.right)
if isinstance(left, (int, float)) and isinstance(right, complex):
if isinstance(node.op, Add):
return left + right
else:
return left - right
return _convert_signed_num(node)
return _convert(node_or_string)
And call it like this:
myList = stringToList(myStringList)
print(myStringList[0] # [
print(myList[0]) # foo
Or I could simply use the wonderful ast package in the default python library and achieve the same results:
import ast
myList = ast.literal_eval(myStringList)
print(myList[0]) # foo
All credit for the stringToList function goes to the creators of the ast package, as I merely copied it from source code.
To add an example of a "good reason" to create your own package/framework, lets say I need to use the eval function for whatever reason. As we all know, eval is dangerous and, generally speaking, shouldn't be used. If I really need to use it, and there aren't any alternatives (cough cough, literal_eval), the only solution would be to create my own version of the eval function, to avoid having exploitable code.
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