Python Forum
Where is "switch" statement ? Do nobody needs it ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where is "switch" statement ? Do nobody needs it ?
#18
@nilamo, seems, that's the same like in Coffeescript.
From reference:
switch day
  when "Mon" then go work
  when "Tue" then go relax
  when "Thu" then go iceFishing
  when "Fri", "Sat"
    if day is bingoDay
      go bingo
      go dancing
  when "Sun" then go church
  else go work
How I used it:
		switch current_json.message
			when "receive_and_answer_secret_question"
				do gatekeeper.ask___secret_question
			when "login_successful"
				alert "Login successfull.
					\nAsking main menu page"
				document.location = "/main_menu/main_menu_page/"
			when "login_failed"
				alert "Login failed.
					\nPlease, check username and password."
How I used it too:
	switch document.location.host
		when "vk.com"
			switch document.location.pathname
				when "/"
					report_to_server = "login_page_general without_anything_other"
				when "/login"
					if document.location.search.includes "act=mobile"
						report_to_server = "login_page_general after_logging_out"
					else if document.location.search.includes "act=blocked"
						report_to_server = "login_page_general_error account_blocked"
					else
						report_to_server = "login_page_general with_login_after_slash"
				when "/restore"
					if document.location.search.includes "act=cancel"
						report_to_server = "login_page_general_error login_data_changed"
					else
						report_to_server = "error_page"
				when "/login.php"  # can be redirected to news or to login_error page
					setTimeout main_function, 5000
					return null
				when "/feed"
					report_to_server = "news_page"
				when "/blank.html"
					setTimeout main_function, 5000
					return null
				else
					report_to_server = "other_page"
		when "oauth.vk.com"
			switch document.location.pathname
				when "/authorize", "/oauth/authorize"
					if document.getElementsByClassName("oauth_app_photo").length > 0
						# earlier was: document.body.innerHTML.includes "запрашивает доступ к Вашему аккаунту"
						report_to_server = "api_app_rights_request_page"
					else if document.getElementsByClassName("oauth_captcha").length > 0
						report_to_server = "captcha_asked"
					else
						report_to_server = "login_page_api"
				else
					report_to_server = "other_page"
		when "api.vk.com"
			if (document.location.pathname == "/blank.html") and (document.location.hash.includes "access_token=")
				report_to_server = "api_logined_page"
			else
				report_to_server = "other_page"
		else
			report_to_server = "other_page"
It's even more often-used statement than "if" =)

Here I writed my thoughts about good syntax and how to implement it: https://github.com/vlad1777d/pythonscript
If somebody interested - you can tell your opinion about it.
Readme is not finished, especially, I had not written about "switch", but I will =)
I think, that "switch" and "if" must be the same like in Coffeescript, to be able to do so:
a = switch b
    when 4
        8 + 8
    when 8, 9, 10
         somevar

a = if b > 0
    4
else
    8
Reply


Messages In This Thread
RE: Where is "switch" statement ? Do nobody needs it ? - by vlad1777d - Nov-29-2017, 08:09 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020