Can this code, to convert string to integer, be made more compact? Problem:
String to integer(leetcode)

Implement atoi which converts a string to an integer.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned.
Note:

Only the space character ' ' is considered as a whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [-2^31, 2^31-1]. If the numerical value is out of the range of representable values, (2^31-1) or -2^31 is returned.


My solution:
I have implemented this code below. I have noted down the requirements and implemented the code. It was hard to keep track of all the details. Before acceptance, I got WA 4 times(and it seems like the code I've written is not compact).
class Solution:
    def myAtoi(self, s: str) -&gt; int:
            l = len(s)
            if l==0:
                print(1)
                return 0
            if l==1:
                if s==&quot; &quot; or s==&quot;+&quot; or s==&quot;-&quot;:
                    return 0
                elif(s.isnumeric()):
                    return int(s)
                else:
                    return 0
            res = &quot;&quot;
            i = 0
            while(i&lt;l and s[i]==&quot; &quot;):
                i+=1
                if i==l:
                    return 0
            ispos = False
            if(s[i]==&quot;+&quot;):
                ispos = True
                i+=1
            isnegative = False
            if(s[i]=='-' and not ispos):
                isnegative = True
                i+=1
            if(i&lt;len(s) and not s[i].isnumeric() and s[i]!='+' and s[i]!='-'):
                return 0
            while(i&lt;l):
                if s[i].isnumeric():
                    res += s[i]
                else:
                    break
                i+=1 
            if len(res)&gt;0:
                x = int(res)
            else:
                return 0
            if isnegative:
                if x&gt;=(1&lt;&lt;31):
                    return -(1&lt;&lt;31)
                x *= -1
            if x&gt;=((1&lt;&lt;31) - 1):
                return (1&lt;&lt;31)-1
            return x

Can anyone write a compact code for this problem?

I also like to know if there is an approach to solve this kind of implementation problems so that I can write compact code. Can anyone share any better method to analyze this type of implementation problem? (or, any suggestion to be better in solving this kind of problems where we need to keep track of many things)
Styling

Try to make the code follow the PEP 8 Style Guidelines, this will make your code follow the Python conventions, and also more readable. So do 
if l == 1 instead of if l==1, my_atoi instead of myAtoi, etc

Code structure

It seems that page requires the code to be inside a class. Leaving the exercise aside for a moment:
You don't need that function inside a class, because you are never using anything of the class itself. It makes more sense to define that method outside the class, and without the (unused) self parameter.

Skiping whitespaces

while(i&lt;l and s[i]==" "):
    i+=1
    if i==l:
        return 0



You don't need those parenthesis on the while in Python.
Your code might not detect some whitespace characters like \t (it seems the exercise accepts that though); use the method isspace(). 
Instead of checking i == l on every iteration (when it could only happen in the last one), check it once you are out of the loop. This allows us to remove the special treatment to empty strings: if s is empty, it won't enter the loop and i == l, so it will return 0.


while i &lt; l and s[i].isspace():
    i += 1

if i == l:
    return 0


Sign

Your code to handle the sign is too complicated. Instead of keeping ispos and isnegative, you could just keep a factor that is either 1 or -1. So instead of 

ispos = False
if(s[i]=="+"):
    ispos = True
    i+=1
isnegative = False
if(s[i]=='-' and not ispos):
    isnegative = True
    i+=1


you can do

factor = -1 if s[i] == '-' else 1
# Skip the sign character if necessary
if s[i] == '-' or s[i] == '+':
    i += 1


Reading the number

if(i&lt;len(s) and not s[i].isnumeric() and s[i]!='+' and s[i]!='-'):
    return 0
while(i&lt;l):
    if s[i].isnumeric():
        res += s[i]
    else:
        break
    i+=1



If we have already parsed the sign, why accept another sign again? 
Be careful, isnumeric() accepts things like ½, which you probably don't want; use isdigit() instead.
EDIT: As noted in the comments, isdigit() will accept things like "¹". So it's probably better to just do s[i] in '0123456789'
It's good practice (and helps avoid unexpected bugs) to declare variables the closest possible to where they are used. You declare res = "" at the beginning of your function, but haven't used it until now.
The while and if can be condensed into a single, more compact loop:


res = ""
while i &lt; l and s[i].isdigit():
    res += s[i]
    i += 1

# You can check if an string is empty doing this:
if not res:
    return 0


Arrived to this point, if the function hasn't returned, we know res contains a number. The way you handle the valid range, before applying the sign, is not very intuitive; it's better if you first apply the sign.

x = factor * int(res)
if x &gt; (1 &lt;&lt; 31):
    return (1&lt;&lt;31)
elif x &lt; -((1 &lt;&lt; 31) - 1):
    return (1 &lt;&lt; 31) - 1
else:
    return x


Or it might be even better to clamp your number using min and max:

return max(-(1 &lt;&lt; 31) + 1, min(1 &lt;&lt; 31, factor * int(res)))


Final code

I have removed the special case of l == 1 because it is already handled by the rest of the code. This is my final code (untested):

def my_atoi(s: str) -&gt; int:
    l = len(s)

    while i &lt; l and s[i].isspace():
        i += 1

    if i == l:
        return 0

    factor = -1 if s[i] == '-' else 1
    # Skip the sign character if necessary
    if s[i] == '-' or s[i] == '+':
        i += 1

    res = ""
    while i &lt; l and s[i].isdigit():
        res += s[i]
        i += 1

    if not res:
        return 0

    return max(-(1 &lt;&lt; 31) + 1, min(1 &lt;&lt; 31, factor * int(res)))