Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Sunday, March 29, 2020

eval() Function in Python

eval function takes a String and evaluate the result.

Examples:
x = eval("10 + 20 + 30")
print(x)
Output:
60
--------------------------
x = eval(input("Enter Expression: "))
print(x)

Output:
Enter Expression: 10+20-5*10/2
5.0

-------------------------

eval() can evaluate the input to list, tuple, set, etc based on the provided input:

i = eval(input("Enter List: "))
print(type(i))
print(i)

Output
Enter List: [10,20,40]
<class 'list'>
[10, 20, 40]

Saturday, March 28, 2020

find maximum of 3 numbers using python

Maximum:
a = int(input("Enter First Number: "))
b = int(input("Enter Second Number: "))
c = int(input("Enter Third Number: "))
min = a if a > b and a > c else b if b > c else c
print("Minimum Value:", min)
Output:
Enter First Number: 10
Enter Second Number: 20
Enter Third Number: 30
Minimum Value: 30

Process returned 0 (0x0)    execution time : 67.708 s
Press [ENTER] to continue...
 

Find the Minimum of 3 numbers using Python

minimum.py
a = int(input("Enter First Number: "))
b = int(input("Enter Second Number: "))
c = int(input("Enter Third Number: "))
min = a if a < b and a < c else b if b < c else c
print("Minimum Value:", min)


Output:
Enter First Number: 10
Enter Second Number: 20
Enter Third Number: 30
Minimum Value: 10

Process returned 0 (0x0)    execution time : 5.773 s
Press [ENTER] to continue...

Sunday, March 15, 2020

ModuleNotFoundError: No module named 'base64'

Error:

└─> $  sudo dnf update
Traceback (most recent call last):
  File "/bin/dnf", line 57, in <module>
    from dnf.cli import main
  File "/usr/lib/python3.6/site-packages/dnf/__init__.py", line 23, in <module>
    import dnf.pycomp
  File "/usr/lib/python3.6/site-packages/dnf/pycomp.py", line 23, in <module>
    import base64
ModuleNotFoundError: No module named 'base64'


Solution:

Download the base64.py from the "https://github.com/python/cpython/blob/3.8/Lib/base64.py" and copied it into the /usr/lib64/python3.6/".

Wednesday, March 11, 2020

Read and Updated the content of a file of a GitLab with GitLab API using Python

 Below is the script to Read and Updated the content of the files in GitLab with GitLab API using Python

You need to install the  "gitlab" Python package using pip before run the script.

#### install gitlab python package with root user
### pip install gitlab

import gitlab
import base64

gl = gitlab.Gitlab('https://xxxxxxxxxxxxxxxxxxxx/', private_token='xxxxxxxxxxxxxxxx')
gl.auth()

project = gl.projects.get(PROJECT_ID)  ####You can get the project PROJECT_ID from GITLab repo

f = project.files.get(file_path='pom.xml', ref='master')
file_content = f.decode().decode('utf-8')

print("====================BEFORE UPDATE================")
print(file_content)

old_line = '<groupId>com.test3</groupId>'
new_line = '<groupId>com.test4</groupId>'
new_file = file_content.replace(old_line, new_line)

print("====================AFTER UPDATE================")
print(new_file)

### COMMIT the changes to GIT
data = {
'branch': 'master',
'commit_message': 'TEST GITLABAPI',
'actions': [
{
'action': 'update',
'file_path': 'pom.xml',
'content': new_file,
}
]
}
commit = project.commits.create(data)