-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
48 lines (44 loc) · 10.1 KB
/
Copy pathapp.py
File metadata and controls
48 lines (44 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from docarray import Document, DocumentArray
from jina import Executor
encoder = Executor.load_config(
'DPRTextEncoder/config.yml', uses_with={'device': 'cuda'})
indexer = Executor.load_config(
'SimpleIndexer/config.yml', uses_metas={'workspace': './workspace'})
ranker = Executor.load_config(
'DPRReaderRanker/config.yml', uses_metas={'workspace': './workspace'})
da = DocumentArray([
Document(text="""Python is a widely used general-purpose, high level programming language. It was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with an emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines of code.Python is a programming language that lets you work quickly and integrate systems more efficiently.There are two major Python versions: Python 2 and Python 3. Both are quite different.Beginning with Python programming:1) Finding an Interpreter:Before we start Python programming, we need to have an interpreter to interpret and run our programs. There are certain online interpreters like https://ide.geeksforgeeks.org /, http://ideone.com / or http://codepad.org / that can be used to run Python programs without installing an interpreter.Windows: There are many interpreters available freely to run Python scripts like IDLE(Integrated Development Environment) that comes bundled with the Python software downloaded from http://python.org/.Linux: Python comes preinstalled with popular Linux distros such as Ubuntu and Fedora. To check which version of Python you’re running, type “python” in the terminal emulator. The interpreter should start and print the version number.macOS: Generally, Python 2.7 comes bundled with macOS. You’ll have to manually install Python 3 from http://python.org/.2) Writing our first program:Just type in the following code after you start the interpreter. # Script Begins print(""GeeksQuiz"") # Scripts Ends Output:GeeksQuizLet’s analyze the script line by line.Line 1: [# Script Begins] In Python, comments begin with a #. This statement is ignored by the interpreter and serves as documentation for our code.Line 2: [print(“GeeksQuiz”)] To print something on the console, print() function is used. This function also adds a newline after our message is printed(unlike in C). Note that in Python 2, “print” is not a function but a keyword and therefore can be used without parentheses. However, in Python 3, it is a function and must be invoked with parentheses.Line 3: [# Script Ends] """),
Document(text='''Python was developed by Guido van Rossum in the early 1990s and its latest version is 3.7.1, we can simply call it as Python3. Python 3.0 was released in 2008. and is interpreted language i.e it’s not compiled and the interpreter will check the code line by line. This article can used to learn very basics of Python programming language.So before moving on further.. let’s do the most popular ‘HelloWorld’ tradition 😛 and hence compare Python’s Syntax with C, C++, Java ( I have taken these 3 because they are most famous and mostly used languages).# Python code for "Hello World"# nothing else to type...see how simple is the syntax.  print("Hello World")      Note: Please note that Python for its scope doesn’t depend on the braces ( { } ), instead it uses indentation for its scope.Now moving on further Lets start our basics of Python . I will be covering the basics in some small sections. Just go through them and trust me you’ll learn the basics of Python very easily.Introduction and SetupIf you are on Windows OS download Python by Clicking here and now install from the setup and in the start menu type IDLE.IDLE, you can think it as an Python’s IDE to run the Python Scripts.It will look somehow this :If you are on Linux/Unix-like just open the terminal and on 99% linux OS Python comes preinstalled with the OS.Just type ‘python3’ in terminal and you are ready to go.It will look like this :The †>>> †represents the python shell and its ready to take python commands and code.Variables and Data StructuresIn other programming languages like C, C++, and Java, you will need to declare the type of variables but in Python you don’t need to do that. Just type in the variable and when values will be given to it, then it will automatically know whether the value given would be an int, float, or char or even a String.# Python program to declare variablesmyNumber = 3print(myNumber)  myNumber2 = 4.5print(myNumber2)  myNumber ="helloworld"print(myNumber)Output:3
4.5
helloworld
# Python program to illustrate a list   # creates a empty listnums = []   # appending data in listnums.append(21)nums.append(40.5)nums.append("String")  print(nums)Output:[21, 40.5, String]Comments:# is used for single line comment in Python
See, how simple is it, just create a variable and assign it any value you want and then use the print function to print it. Python have 4 types of built in Data Structures namely List, Dictionary, Tuple and Set.List is the most basic Data Structure in python. List is a mutable data structure i.e items can be added to list later after the list creation. It’s like you are going to shop at the local market and made a list of some items and later on you can add more and more items to the list.append() function is used to add data to the list.
""" this is a comment """ is used for multi line commentsInput and OutputIn this section, we will learn how to take input from the user and hence manipulate it or simply display it. input() function is used to take input from the user. # Python program to illustrate# getting input from username = input("Enter your name: ")   # user entered the name 'harssh'print("hello", name)Output:hello harssh # Python3 program to get input from user  # accepting integer from the user# the return type of input() function is string ,# so we need to convert the input to integernum1 = int(input("Enter num1: "))num2 = int(input("Enter num2: "))  num3 = num1 * num2print("Product is: ", num3)Output:Enter num1: 8 Enter num2: 6 ('Product is: ', 48)
SelectionSelection in Python is made using the two keywords ‘if’ and ‘elif’ and else (elseif) # Python program to illustrate# selection statement  num1 = 34if(num1>12):    print("Num1 is good")elif(num1>35):    print("Num2 is not gooooo....")else:    print("Num2 is great")Output:Num1 is goodFunctionsYou can think of functions like a bunch of code that is intended to do a particular task in the whole Python script. Python used the keyword ‘def’ to define a function.Syntax:def function-name(arguments):
#function body# Python program to illustrate# functionsdef hello():Â Â Â Â print("hello")Â Â Â Â print("hello again")hello()Â Â # calling functionhello()Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Output:hello
hello again
hello
hello again
# Python program to illustrate # function with maindef getInteger():    result = int(input("Enter integer: "))    return result  def Main():    print("Started")      # calling the getInteger function and     # storing its returned value in the output variable    output = getInteger()         print(output)  # now we are required to tell Python # for 'Main' function existenceif __name__=="__main__":    Main()Output:Started
Now as we know any program starts from a ‘main’ function…lets create a main function like in many other programming languages.
Enter integer: 5
# Python program to illustrate# a simple for loop  for step in range(5):        print(step)Output:0
Iteration(Looping)As the name suggests it calls repeating things again and again. We will use the most popular ‘for’ loop here.
1
2
3
4
# Python program to illustrate# math moduleimport math  def Main():    num = -85      # fabs is used to get the absolute     # value of a decimal    num = math.fabs(num)     print(num)            if __name__=="__main__":    Main()Output:85.0These are some of the most basics of the Python programming language and I will be covering both the intermediate and advanced level Python topics in my upcoming articles.This article is contributed by Harsh Wardhan Chaudhary. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks. Your article will be reviewed first by Geeks for Geeks team before publishing.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.My Personal Notes
ModulesPython has a very rich module library that has several functions to do many tasks. You can read more about Python’s standard library by Clicking here‘import’ keyword is used to import a particular module into your python code. For instance consider the following program.
arrow_drop_upSave'''),
Document(text=r"sum() Sums the items of an iterator"),
Document(text="abs() Returns the absolute value of a number"),
Document(text="tuple() Returns a tuple")])
encoder.encode(docs=da)
indexer.index(docs=da)
q_da = DocumentArray([Document(text='convert a string to a tuple')])
encoder.encode(docs=q_da)
indexer.search(docs=q_da)
ranker.rank(docs=q_da)
for m in q_da[0].matches:
print(f'score: {m.scores["relevance_score"].value:.4f}, text: {m.text}')